package com.bz.xtcx.manager.controller;

import com.bz.xtcx.manager.comom.XtcxException;
import com.bz.xtcx.manager.service.IDataService;
import com.bz.xtcx.manager.utils.WzDateUtil;
import com.bz.xtcx.manager.vo.VoCheck;
import com.bz.xtcx.manager.vo.VoResponse;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("data")
public class DataController {
    /** 日志. */
    private final Logger logger = LoggerFactory.getLogger(DataController.class);

	@Autowired
	private IDataService dataService;
	
	@GetMapping("user")
	public Object user() {
		VoResponse voRes = new VoResponse();
		voRes.setData(dataService.getUserStatistics());
		return voRes;
	}
	
	@PostMapping("user")
	public Object userBy(@RequestBody VoCheck check) {
		VoResponse voRes = new VoResponse();
		if(check != null) {
			Map<String, Object> map = null;
//			if(check.getSite() == null || !check.getSite().equals("1")) {
//				check.setIfSite(false);
//			}else {
//				check.setIfSite(true);
//				if(check.getSites() == null) {
//					check.setSites(new ArrayList<String>());
//				}
//			}
			if(check.getType() == null || (!check.getType().equals("1") && !check.getType().equals("2") && !check.getType().equals("3") && !check.getType().equals("4"))) {
				check.setIfType(false);
			}else {
				check.setIfType(true);
			}
			map = dataService.getUserStatistics(check);
			voRes.setData(map);
		}
		return voRes;
	}
	
	@GetMapping("lib")
	public Object lib(@RequestParam(name="formType") int formType) {
		VoResponse voRes = new VoResponse();
		if(formType < 1 || formType > 7) {
			voRes.setFail(voRes);
			voRes.setMessage("参数错误");
			return voRes;
		}
		voRes.setData(dataService.getLib(formType));
		return voRes;
	}
	
	@PostMapping("lib")
	public Object libBy(@RequestParam(name="formType") int formType, @RequestBody VoCheck check) {
		VoResponse voRes = new VoResponse();
		if(check.getSite() == null || (!check.getSite().equals("1") && !check.getSite().equals("2") && !check.getSite().equals("3"))) {
			check.setIfSite(false);
		}else {
			check.setIfSite(true);
			if(check.getSites() == null) {
				check.setSites(new ArrayList<String>());
			}
		}
		if(check.getDomain() == null ||  !check.getDomain().equals("1")) {
			check.setIfDomain(false);
		}else {
			check.setIfDomain(true);
		}
		switch(formType) {
			case 1 :
				if(check.getSex() == null ||  !check.getSex().equals("1")) {
					check.setIfSex(false);
				}else {
					check.setIfSex(true);
				}
				if(check.getEducation() == null ||  !check.getEducation().equals("1")) {
					check.setIfEducation(false);
				}else {
					check.setIfEducation(true);
				}
				if(check.getDegree() == null ||  !check.getDegree().equals("1")) {
					check.setIfDegree(false);
				}else {
					check.setIfDegree(true);
				}
				break;
			case 2 :
				if(check.getScale() == null ||  !check.getScale().equals("1")) {
					check.setIfScale(false);
				}else {
					check.setIfScale(true);
				}
				if(check.getRegType() == null ||  !check.getRegType().equals("1")) {
					check.setIfRegType(false);
				}else {
					check.setIfRegType(true);
				}
				if(check.getIsListed() == null ||  !check.getIsListed().equals("1")) {
					check.setIfIsListed(false);
				}else {
					check.setIfIsListed(true);
				}
				break;
			case 3 :
				if(check.getNature() == null ||  !check.getNature().equals("1")) {
					check.setIfNature(false);
				}else {
					check.setIfNature(true);
				}
				if(check.getCategory() == null ||  !check.getCategory().equals("1")) {
					check.setIfCategory(false);
				}else {
					check.setIfCategory(true);
				}
				break;
            case 6 :
                if (check.getStage() == null || !check.getStage().equals("1")) {
                    check.setIfStage(false);
                } else {
                    check.setIfStage(true);
                }
                break;
			case 4 :
			case 7 :
				break;
			default :
				voRes.setFail(voRes);
				voRes.setMessage("参数错误");
				return voRes;
		}
        try {
		    voRes.setData(dataService.getLibStatistics(formType, check));
        } catch (XtcxException e) {
            logger.error("资源库统计处理失败", e);
            voRes.setFail(voRes);
            voRes.setMessage("资源库统计处理失败");
            return voRes;
        } catch (Exception e) {
            logger.error("资源库统计处理系统异常", e);
            voRes.setFail(voRes);
            voRes.setMessage("资源库统计处理系统异常");
            return voRes;
        }
		return voRes;
	}

	@GetMapping("meeting")
	public VoResponse countMeeting() {
		VoResponse voRes = new VoResponse();
		voRes.setData(dataService.getDjStatistics());
		return voRes;
	}

	// add by wangtao on 20190103 for 对接可以按时间过滤后再进行统计 start
    @ApiOperation(value = "对接数据统计接口(可根据时间过滤)", httpMethod = "GET", notes = "对接数据统计接口(可根据时间过滤)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDateTime", value = "查询开始时间(时间戳)", dataType = "Long", required = false, paramType = "query"),
            @ApiImplicitParam(name = "endDateTime", value = "查询结束时间(时间戳)", dataType = "Long", required = false, paramType = "query")
    })
    @GetMapping("/meetingbytime")
    public VoResponse countMeetingByTime(@RequestParam(value = "startDateTime", required = false) Long startDateTime, @RequestParam(value = "endDateTime", required = false) Long endDateTime) {
        VoResponse voRes = new VoResponse();
        String usedStTime = null;
        String usedEdTime = null;
        if (null != startDateTime && null != endDateTime && startDateTime > endDateTime) {
        	voRes.setFail(voRes);
        	voRes.setMessage("查询结束时间不应早于查询起始时间");
        	return voRes;
		}
        if (null != startDateTime) {
            usedStTime = WzDateUtil.dt2Str(WzDateUtil.dayStart(new Date(startDateTime)), WzDateUtil.DT_FMT_5);
        }
        if (null != endDateTime) {
			usedEdTime = WzDateUtil.dt2Str(WzDateUtil.dayStart(new Date(endDateTime)), WzDateUtil.DT_FMT_5);
        }
        voRes.setData(dataService.getDjStatisticsByTime(usedStTime, usedEdTime));
        return voRes;
    }
    // add by wangtao on 20190103 for 对接可以按时间过滤后再进行统计 end

    @GetMapping("visiters")
    public VoResponse countVisiters() {
        VoResponse voRes = new VoResponse();
        voRes.setData(dataService.getVisiterStatistics());
        return voRes;
    }
}
